package com.fmsh.nfcsensor.tools.chart;

import android.content.Context;
import android.graphics.Color;
import android.os.SystemClock;


import com.fmsh.base.utils.UIUtils;
import com.fmsh.nfcsensor.R;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;

import java.util.List;

public class LineChartManagerUtils {
    private LineChart lineChart;
    private final YAxis axisLeft;
    private final YAxis axisRight;
    private final XAxis xAxis;
    private LineDataSet lineDataSet;
    private LineData lineData;
    private Context mContent;
    public LineChartManagerUtils(Context context, LineChart lineChart){
        this.lineChart = lineChart;
        this.mContent = context;
        axisLeft = lineChart.getAxisLeft();
        axisRight = lineChart.getAxisRight();
        xAxis = lineChart.getXAxis();
        init();
    }

    private void init(){
        lineChart.setDrawGridBackground(false);
        lineChart.setNoDataText("暂无数据");
        lineChart.setPinchZoom(false);
        lineChart.setScaleEnabled(false);
        lineChart.getDescription().setEnabled(false);
        lineChart.getAxisRight().setEnabled(false);
        lineChart.getAxisLeft().setEnabled(true);
        Description description = new Description();
        //        description.setText("需要展示的内容");
        description.setEnabled(false);
        lineChart.setDescription(description);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setEnabled(true);
        xAxis.setGranularity(1f);
        xAxis.setDrawGridLines(false);
        xAxis.setDrawLabels(false);
        xAxis.setAxisMinimum(0);
        axisLeft.setAxisMinimum(0);
        axisRight.setAxisMinimum(0);
        lineChart.getLegend().setEnabled(false);
        initLineDataSet();
    }

    public void initLegend(String label){
        //设置图例
        Legend legend = lineChart.getLegend();
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);//设置显示在顶部
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);//设置图例的显示位置为居中
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);//设置图例显示的布局为横线排列
        legend.setTextSize(10f);//设置图例文本的字体大小，根据界面显示自行调配
        legend.setEnabled(true);
        lineDataSet.setLabel(label);
        lineChart.invalidate();
    }

    public void initLineDataSet(){
        lineDataSet = new LineDataSet(null, null);
        lineDataSet.setColor(UIUtils.getColor(mContent, R.color.light_blue));
        lineDataSet.setLineWidth(1.5f);
        lineDataSet.setCircleRadius(1.5f);
        lineDataSet.setColor(UIUtils.getColor(mContent, R.color.light_blue));
        lineDataSet.setDrawFilled(false);
        lineDataSet.setCircleColor(UIUtils.getColor(mContent, R.color.light_blue));
        lineDataSet.setHighLightColor(UIUtils.getColor(mContent, R.color.light_blue));
        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        lineDataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        lineDataSet.setValueTextSize(10f);
        xAxis.setDrawGridLines(false);

        //添加一个空的 LineData
        lineData = new LineData();
        lineChart.setData(lineData);
        lineChart.invalidate();
    }

    public void setYAxis(float max, float min, int labelCount) {
        if (max < min) {
            return;
        }
        axisLeft.setAxisMaximum(max);
        axisLeft.setAxisMinimum(min);
        axisLeft.setLabelCount(labelCount, false);
        lineChart.invalidate();
    }


    public void addEntry(List<Float> numbers) {
        addEntry(numbers,false);

    }
    public void addEntry(List<Float> numbers,boolean isInt) {

        lineData = new LineData(lineDataSet);
        lineData.setValueFormatter(new ValueFormatter() {
            @Override
            public String getFormattedValue(float value) {
                if (isInt){
                    return String.format("%.0f",value);
                }else {
                    return String.format("%.2f",value);

                }
            }
        });
        lineChart.setData(lineData);


//        for (int i = 0; i < numbers.size(); i++) {
//            Entry entry = new Entry(System.currentTimeMillis(),numbers.get(i).floatValue());
//            lineData.addEntry(entry, i);
//            lineData.notifyDataChanged();
//            lineChart.notifyDataSetChanged();
//            lineChart.setVisibleXRangeMaximum(6);
//            lineChart.moveViewToX(lineData.getEntryCount() - 5);
//        }
    }
    /**
     * 显示无数据的提示
     *
     */
    public  void showNoDataText() {
        lineChart.clear();
        lineChart.notifyDataSetChanged();
        lineChart.setNoDataTextColor(Color.parseColor("#999999"));
        lineChart.setNoDataText("暂无数据");
        lineChart.invalidate();
    }

}
